﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar.Metro.ColorTables;
using BLL;
using BOL;
using DevComponents.AdvTree;
using System.IO;
using System.Drawing.Imaging;

namespace QLThueMuonXe
{
    public partial class frmNhanVien : UserControl
    {
        FormNhanVien fnv = new FormNhanVien();
        HamDungChung dc = new HamDungChung();
        List<ThongTinNhanVien> ttnv = new List<ThongTinNhanVien>();
        string imagename;//lưu tên của hình

        public frmNhanVien()
        {
            InitializeComponent();
            cobchucvu.Items.Add("Quản lý");
            cobchucvu.Items.Add("Nhân viên");
            cobchucvu.Text = "Nhân viên";

            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            cobgioitinh.Items.Add("Khác");
            cobgioitinh.Text = "";
        }

        #region Điều khiển đóng/mở form
        private HamDungChung _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public HamDungChung Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    HamDungChung oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(HamDungChung oldValue, HamDungChung newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmNhanVien;
            }
            else
            {
                btndong.Command = null;
            }
        }
        #endregion

        public void xoatrang()
        {
            txtmanv.Text = dc.NextID(fnv.madatNVlonnhat(), "NV");
            txthoten.Text = "";
            dateTimengaysinh.Text = "";
            cobgioitinh.Text = "";
            txtdiachi.Text = "";
            txtsodt.Text = "";
            txttendn.Text = "";
            txtmatkhau.Text = "";
            txttendn.ReadOnly = false;
            panel1.Visible = true;
            linkLabel1.Visible = false;
            cobchucvu.Text = "Nhân viên";
            chkKhoa.Checked = false;
            this.picanhdaidien.Image = global::QLThueMuonXe.Properties.Resources.no_avatar_0bee7320cb7ab3dca5de0130ab3aec46;            
        }

        public void huybo()
        {
            txttendn.ReadOnly = true;
            panel1.Visible = false;
            linkLabel1.Visible = true;            
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if(btnthem.Text == "Thêm Mới")
            {
                btnthem.Text = "Lưu";
                btnsua.Text = "Hủy Bỏ";
                xoatrang();
                this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txttendn.Text == "" || cobchucvu.Text == "" || txttendn.Text =="" || txtmatkhau.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (!fnv.checktendn(txttendn.Text))
                        MessageBox.Show("Tên đăng nhập đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                    {
                        MemoryStream stream = new MemoryStream();
                        picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                        fnv.themnv(txtmanv.Text, txthoten.Text, dateTimengaysinh.Value, cobgioitinh.Text, txtsodt.Text, txtdiachi.Text, txttendn.Text, dc.EncodeMD5(txtmatkhau.Text.Trim()), cobchucvu.Text, chkKhoa.Checked, stream);
                        MessageBoxEx.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnthem.Text = "Thêm Mới";
                        btnsua.Text = "Sửa";
                        nvLoad();
                        this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
                    }
                }
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if(btnsua.Text == "Sửa")
            {
                MemoryStream stream = new MemoryStream();
                picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                fnv.suanv(txtmanv.Text, txthoten.Text, dateTimengaysinh.Value, cobgioitinh.Text, txtsodt.Text, txtdiachi.Text, cobchucvu.Text, chkKhoa.Checked, stream);
                MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nvLoad();
            }
            else
            {
                btnthem.Text = "Thêm Mới";
                btnsua.Text = "Sửa";
                huybo();                
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
                advTree1.SelectedIndex = advTree1.SelectedIndex + 1;
                
            }
        }

        private Node CreateNVRow(string manv, string hoten, string ngay, string sdt, string diachi, string chucvu)
        {
            Node node = new Node(manv);
            node.Cells.Add(new Cell(hoten));
            node.Cells.Add(new Cell(ngay));
            node.Cells.Add(new Cell(sdt));
            node.Cells.Add(new Cell(diachi));
            node.Cells.Add(new Cell(chucvu));
            return node;
        }


        public void nvLoad()
        {
            List<ThongTinNhanVien> ttnv = fnv.LayDSNhanVien();
            advTree1.Nodes.Clear();
            foreach (var row in ttnv)
            {
                advTree1.Nodes.Add(CreateNVRow(row.manv, row.hotennv, row.ngaysinhnv.ToString("dd/MM/yyyy"), row.sdtnv, row.diachinv, row.chucvu));
            }
            advTree1.SelectedIndex = 0;
        }
        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            nvLoad();            
        }

        private void btnduyetdanh_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog dldlg = new OpenFileDialog();
                dldlg.InitialDirectory = @":d\";
                dldlg.Filter = "Image File(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (dldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = dldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                    picanhdaidien.Image = (Image)newimg;
                }
                dldlg = null;

            }
            catch (System.ArgumentException ae)
            {
                imagename = " ";
                MessageBox.Show(ae.Message.ToString());

            }
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
                Node node = e.Node;
                try
                {
                ttnv = fnv.LayDSNhanVien1(node.Cells[0].Text.Trim());
                }
                catch (Exception) { ttnv = fnv.LayDSNhanVien1("NV01"); }
                txtmanv.Text = ttnv.First().manv;
                txthoten.Text = ttnv.First().hotennv;
                dateTimengaysinh.Value = ttnv.First().ngaysinhnv;
                cobgioitinh.Text = ttnv.First().gioitinhnv;
                txtdiachi.Text = ttnv.First().diachinv;
                txtsodt.Text = ttnv.First().sdtnv;
                txttendn.Text = ttnv.First().tendangnhap.Trim();
                txtmatkhau.Text = ttnv.First().matkhau;
                cobchucvu.Text = ttnv.First().chucvu;
                chkKhoa.Checked = ttnv.First().khoa;
                MemoryStream str = fnv.layhinhnv(txtmanv.Text);
                Image img = Image.FromStream(str);
                if (img == null) return;
                picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                picanhdaidien.Image = img;
            
        }

        private void txtsodt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDoiMK_QL frm = new frmDoiMK_QL();
            frm.manv = txtmanv.Text;
            frm.ShowDialog();
        }
    }
}
